﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace Airborne.ScrumPoker.Module.TeamFoundation.Model
{
    public class TeamFoundationConfiguration 
    {

        #region Fields
        private const string MicrosoftFolderName = "Microsoft";
        private const string TeamFoundationFolderName = "Team Foundation Server";
        private const string TeamFoundationServerConfig = "TFSServerManager.config";
        
        private IList<string> serverOptions;
        #endregion

        #region Constructor
        public TeamFoundationConfiguration()
        {
        }
        #endregion

        #region Properties
        private static string TeamFoundationConfigDirectory
        {
            get
            {
                string applicationDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), MicrosoftFolderName);
                string configDirectory = System.IO.Path.Combine(applicationDataFolder, TeamFoundationFolderName);
                if (Directory.Exists(configDirectory))
                {
                    return configDirectory;
                }
                return null;
            }
        }

        public string Server { get; set; }

        public IList<string> ServerOptions
        {
            get
            {
                if (serverOptions == null)
                {
                    serverOptions = new List<string>();
                    LoadTeamFoundationServer(serverOptions);
                }
                return serverOptions;
            }
        }

        private static void LoadTeamFoundationServer(IList<string> serverOptions)
        {
            string serverConfigPath = TeamFoundationConfigDirectory;
            if (!string.IsNullOrEmpty(serverConfigPath))
            {
                string path = System.IO.Path.Combine(serverConfigPath, TeamFoundationServerConfig);

                try
                {
                    using (FileStream stream = new FileStream(path, FileMode.Open))
                    {
                        using (XmlTextReader reader = new XmlTextReader(stream))
                        {
                            XmlDocument document = new XmlDocument();
                            document.Load(reader);
                            foreach (XmlElement element in document.ChildNodes)
                            {
                                if (element.Name == "Servers")
                                {
                                    foreach (XmlElement element2 in element.ChildNodes)
                                    {
                                        string name = element2.Name;
                                        if (string.Compare(name, "server", StringComparison.OrdinalIgnoreCase) == 0)
                                        {
                                            serverOptions.Add(element2.Attributes["Url"].Value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (DirectoryNotFoundException)
                {
                }
                catch (FileNotFoundException)
                {
                }
            }
        }
        #endregion
    }
}
